import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../../constants';
import {injectIntl, language} from '../intl';
import WrappedSelect from '../../common/WrappedSelect';
import DealerSelectPanel from '../../common/DealerSelect/SelectEdit';
import WrappedDatePicker from '../../common/WrappedDatePicker';
import TagSelect from 'Shared/components/TagSelect';
import SearchSelectPanel from '../../common/DutyUnitSelect/SelectEdit';
import {lcKind, lcStatus, lcOfficialDraftType} from '../../Enum';
import {getAreas, getAreasSimple} from '../api';
const FormItem = Form.Item;

class QueryHistory extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onChange(name, value);
        this.props.querySearch();
    }
    onQueryReset =() => {
        this.props.onClickReset();
    }
    render() {
        const {formatMessage} = this.props.intl;
        const condition = this.props.condition || {};
        const dealerInfo = condition.dealerId ? {
            id: condition.dealerId,
            code: condition.dealerCode,
            name: condition.dealerName
        } : null;
        const areaInfo = this.props.condition.issueCountryId ? {
            id: this.props.condition.issueCountryId,
            chineseName: this.props.condition.issueCountryChineseName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.dealerCode',
                                defaultMessage: '总代编号'
                            })} {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    value={dealerInfo}
                                    onSelect={this.props.onSelect}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryHistory.form.brand',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="brandId"
                                    value={this.props.condition.brandId}
                                    options={this.props.brands}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.lcCode',
                                defaultMessage: '信用证号码'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="lcCode"
                                    value={this.props.condition.lcCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.beginIssueDate',
                                defaultMessage: '开证日期起'
                            })} {...FORM_OPTIONS.item}>
                                <WrappedDatePicker
                                    name="beginIssueDate"
                                    value={this.props.condition.beginIssueDate}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.endIssueDate',
                                defaultMessage: '开证日期止'
                            })} {...FORM_OPTIONS.item}>
                                <WrappedDatePicker
                                    name="endIssueDate"
                                    value={this.props.condition.endIssueDate}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.issueBank',
                                defaultMessage: '开证银行'
                            })} {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="issueBank"
                                    value={this.props.condition.issueBank}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryHistory.form.issueCountryId',
                                    defaultMessage: '开证国家'
                                })}
                                {...FORM_OPTIONS.item}>
                                <SearchSelectPanel
                                    value={areaInfo}
                                    getDutyUnits={getAreas}
                                    getDutyUnitSimple={getAreasSimple}
                                    titleName={formatMessage({
                                        id: 'queryHistory.form.issueCountryId',
                                        defaultMessage: '开证国家'
                                    })}
                                    isCountry={true}
                                    onSelect={this.props.onSelectArea} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.lcKind',
                                defaultMessage: '信用证类型'
                            })} {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="lcKind"
                                    value={this.props.condition.lcKind}
                                    options={lcKind.toList(language)}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryHistory.form.lcOfficialDraftType',
                                defaultMessage: '正本草本'
                            })} {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="lcOfficialDraftType"
                                    value={this.props.condition.lcOfficialDraftType}
                                    options={lcOfficialDraftType.toList(language)}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary"
                                onClick={this.props.querySearch} loading={this.props.loading}>
                                {formatMessage({
                                    id: 'queryHistory.btn.search',
                                    defaultMessage: '查询'
                                })}</Button>
                            <Button key="clear" onClick={this.onQueryReset}>{formatMessage({
                                id: 'queryHistory.btn.reset',
                                defaultMessage: '重置'
                            })}</Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryHistory.propTypes = {
    brands: PropTypes.array.isRequired,
    condition: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onChange: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
};

import {connect} from 'react-redux';
import {
    saveQueryConditionHistory,
    querySearchHistory,
    resetQueryPanelHistory,
    selectDealerQueryHistory,
    selectAreaQueryHistory
} from '../actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const EMPTY_ARRAY = [];
const getConditions = selectorFactory(['page', 'appState', 'queryHistory']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);

const getBrands = createSelector(
    state => state.getIn(['page', 'domainData', 'initData', 'brands']),
    data => {
        const brands = data.toJS();
        return brands
            ? brands.map(item => ({
                value: item.id,
                text: `(${item.code})${item.name}`
            }))
            : EMPTY_ARRAY;
    }
);

const mapStateToProps = state => ({
    permissions: getPermissions(state),
    condition: getConditions(state),
    loading: state.getIn((['page', 'domainData', 'listHistory', 'isFetching'])),
    brands: getBrands(state),
});

const mapDispatchToProps = dispatch => ({
    onChange: (name, value) => dispatch(saveQueryConditionHistory(name, value)),
    querySearch: () => dispatch(querySearchHistory()),
    onClickReset: () => dispatch(resetQueryPanelHistory()),
    onSelect: data => dispatch(selectDealerQueryHistory(data)),
    onSelectArea: data => dispatch(selectAreaQueryHistory(data)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryHistory));
